package com.jyw.business.ast.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.jyw.business.ast.constant.AstRelationConstant;
import com.jyw.business.ast.domain.AstDealRelation;
import com.jyw.business.ast.domain.AstInfo;
import com.jyw.business.ast.domain.dto.web.AstDisposeDto;
import com.jyw.business.ast.domain.dto.web.AstDisposePageDto;
import com.jyw.business.ast.domain.dto.web.AstRejectedDto;
import com.jyw.business.ast.domain.vo.web.AstDisposeDetailVo;
import com.jyw.business.ast.domain.vo.web.AstDisposePageVo;
import com.jyw.business.ast.domain.vo.web.DisposeAstVo;
import com.jyw.business.ast.service.IAstDealRelationService;
import com.jyw.business.ast.service.IAstInfoService;
import com.jyw.common.utils.DateUtils;
import com.jyw.common.utils.SecurityUtils;
import com.jyw.system.utils.ConversionUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
import com.jyw.business.ast.mapper.AstDisposeMapper;
import com.jyw.business.ast.domain.AstDispose;
import com.jyw.business.ast.service.IAstDisposeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;

/**
 * 资产-处置信息Service业务层处理
 * 
 * @author system
 * @date 2023-10-19
 */
@Service
public class AstDisposeServiceImpl extends ServiceImpl<AstDisposeMapper,AstDispose> implements IAstDisposeService
{
    @Resource
    private AstDisposeMapper astDisposeMapper;

    @Resource
    private IAstDealRelationService relationService;

    @Resource
    private IAstInfoService infoService;

    /**
     * 查询资产-处置信息
     * 
     * @param id 资产-处置信息主键
     * @return 资产-处置信息
     */
    @Override
    public AstDispose selectAstDisposeById(Long id)
    {
        return astDisposeMapper.selectAstDisposeById(id);
    }

    /**
     * 查询资产-处置信息列表
     * 
     * @param astDispose 资产-处置信息
     * @return 资产-处置信息
     */
    @Override
    public List<AstDispose> selectAstDisposeList(AstDispose astDispose)
    {
        return astDisposeMapper.selectAstDisposeList(astDispose);
    }

    /**
     * 新增资产-处置信息
     * 
     * @param astDispose 资产-处置信息
     * @return 结果
     */
    @Override
    public int insertAstDispose(AstDispose astDispose)
    {
        astDispose.setCreateTime(DateUtils.getNowDate());
        return astDisposeMapper.insertAstDispose(astDispose);
    }

    /**
     * 修改资产-处置信息
     * 
     * @param astDispose 资产-处置信息
     * @return 结果
     */
    @Override
    public int updateAstDispose(AstDispose astDispose)
    {
        return astDisposeMapper.updateAstDispose(astDispose);
    }

    /**
     * 批量删除资产-处置信息
     * 
     * @param ids 需要删除的资产-处置信息主键
     * @return 结果
     */
    @Override
    public int deleteAstDisposeByIds(Long[] ids)
    {
        return astDisposeMapper.deleteAstDisposeByIds(ids);
    }

    /**
     * 删除资产-处置信息信息
     * 
     * @param id 资产-处置信息主键
     * @return 结果
     */
    @Override
    public int deleteAstDisposeById(Long id)
    {
        return astDisposeMapper.deleteAstDisposeById(id);
    }

    @Override
    public List<AstDisposePageVo> disposePage(AstDisposePageDto dto) {
        dto.setCreateEndTime(DateUtils.toDateLastTime(dto.getCreateEndTime()));
        return ConversionUtil.conversionList(astDisposeMapper.disposePage(dto),AstDisposePageVo.class);
    }

    @Override
    public AstDisposeDetailVo disposeDetail(Long id) {
        AstDispose dispose = getById(id);
        //读取处置信息
        AstDisposeDetailVo vo = new AstDisposeDetailVo(dispose);
        //读取资产信息
        List<DisposeAstVo> list = relationService.disposeRelation(id);
        vo.setList(list);
        return ConversionUtil.conversion(vo,AstDisposeDetailVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dispose(AstDisposeDto dto) {
        AstDispose dispose = dto.buildDispose();
        //保存处置信息
        save(dispose);
        //封装处置资产信息
        List<AstDealRelation> list = dto.buildRelation(dispose);
        //保存处置资产信息
        relationService.saveBatch(list);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long id) {
        //先删除关系
        relationService.lambdaUpdate()
                .eq(AstDealRelation::getType, AstRelationConstant.DISPOSE)
                .eq(AstDealRelation::getBusinessId,id)
                .remove();
        //再删除处置信息
        removeById(id);
    }

    @Override
    public List<AstDisposePageVo> auditDisposePage(AstDisposePageDto dto) {
        dto.setCreateEndTime(DateUtils.toDateLastTime(dto.getCreateEndTime()));
        return ConversionUtil.conversionList(astDisposeMapper.disposePage(dto),AstDisposePageVo.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void pass(List<Long> list) {
        list = lambdaQuery()
                .in(AstDispose::getId,list)
                .eq(AstDispose::getAuditState,0)
                .list().stream().map(AstDispose::getId).collect(Collectors.toList());
        if(!CollectionUtils.isEmpty(list)){
            //查询所有资产信息
            List<AstDealRelation> relationList = relationService.lambdaQuery()
                    .eq(AstDealRelation::getType, AstRelationConstant.DISPOSE)
                    .in(AstDealRelation::getBusinessId, list)
                    .list();
            //设置资产设备状态
            relationList.forEach(data->{
                infoService.updateById(new AstInfo(){{setId(data.getAstId());setEquipmentState(data.getDispose());}});
            });
            //设置处置审核结果
            lambdaUpdate()
                    .in(AstDispose::getId,list)
                    .set(AstDispose::getAuditState,"2")
                    .set(AstDispose::getAuditUserId,SecurityUtils.getUserId())
                    .update();
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void reject(AstRejectedDto dto) {
        //设置资产信息状态
        lambdaUpdate()
                .eq(AstDispose::getId,dto.getId())
                .set(AstDispose::getAuditState,"1")
                .set(AstDispose::getAuditRemark,dto.getRemark())
                .set(AstDispose::getAuditUserId,SecurityUtils.getUserId())
                .update();
    }
}
